/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao.implement;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import model.dao.database.Database;
import model.dao.entiti.Pasien;
import model.dao.iface.PasienIface;

/**
 *
 * @author compaq
 */
public class PasienImplement implements PasienIface {

    public void tambahPasien(Pasien p) {
        try{
            PreparedStatement ps = Database.getConnection().prepareStatement("INSERT INTO pasien VALUES(?,?,?,?,?,?)");
            ps.setString(1,p.getId());
            ps.setString(2,p.getNama());
            ps.setString(3,p.getUmur());
            ps.setString(4,p.getJenkel());
            ps.setString(5,p.getGoldarah());
            ps.setString(6,p.getAlamat());
            ps.executeUpdate();
        }
        catch(Exception e){
            
        }
    }

    public void hapusPasien(String id) {
        try{
            PreparedStatement ps = Database.getConnection().prepareStatement("DELETE FROM pasien WHERE id=?");
            ps.setString(1, id);
            ps.executeUpdate();
        }
        catch(Exception e){
            
        }
    }

    public Pasien bacaPasien(String id) {
         try{
            PreparedStatement ps = Database.getConnection().prepareStatement("SELECT * FROM pasien WHERE id=?");
            ps.setString(1, id);
            ResultSet rs = ps.executeQuery();
            Pasien p = new Pasien();
            if (rs.next()) {
                p.setId(rs.getString("id"));
                p.setNama(rs.getString("nama"));
                p.setUmur(rs.getString("umur"));
                p.setJenkel(rs.getString("jenkel"));
                p.setGoldarah(rs.getString("goldarah"));
                p.setAlamat(rs.getString("alamat"));
            } 
            return p ;
        }
        catch(Exception e){
            return null ;
        }
    }

    public void ubahPasien(String id, Pasien p) {
        try{
           PreparedStatement ps = Database.getConnection().prepareStatement("UPDATE pasien SET nama=?,umur=?,jenkel=?,goldarah=?,alamat=? WHERE id=?");
           ps.setString(1,p.getNama());
           ps.setString(2,p.getUmur());
           ps.setString(3,p.getJenkel());
           ps.setString(4,p.getGoldarah());
           ps.setString(5,p.getAlamat());
           ps.setString(6,id);
           ps.executeUpdate();
        }
        catch(Exception e){
            
        }
    }

    public List<Pasien> bacaSemuaPasien() {
        try{
            Statement st = Database.getConnection().createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM pasien") ;
            List<Pasien> list = new ArrayList<Pasien>() ;
            while (rs.next()) {
                Pasien p = new Pasien();
                p.setId(rs.getString("id"));
                p.setNama(rs.getString("nama"));
                p.setUmur(rs.getString("umur"));
                p.setJenkel(rs.getString("jenkel"));
                p.setGoldarah(rs.getString("goldarah"));
                p.setAlamat(rs.getString("alamat"));
                list.add(p);
            } 
            return list ;
        }
        catch(Exception e){
           return null; 
        }
    }
    
}
